package io.github.harmonly.skyblock.command;

import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.CommandPermission;
import dev.jorel.commandapi.arguments.*;
import io.github.harmonly.skyblock.manager.DataManager;
import io.github.harmonly.skyblock.manager.ResourcesManager;
import io.github.harmonly.skyblock.util.Replacer;
import io.github.harmonly.skyblock.util.StableObject;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import java.util.Optional;

public class RankCommand implements SBCommand {

    @Override
    public String name() {
        return "rank";
    }

    @Override
    public void register() {
        new CommandAPICommand("rank")
                .withPermission(CommandPermission.OP)
                .withArguments(new PlayerArgument("player"))
                .withArguments(new StringArgument("rank").replaceSuggestions(ArgumentSuggestions.strings(
                        "NONE", "YOUTUBE", "ADMIN"
                )))
                .executes((sender, args) -> {
                    Player player = (Player) args.get(0);
                    String rank = (String) args.get(1);
                    if (player == null || rank == null) {
                        return;
                    }
                    DataManager.getPlayerData(player).ifPresent(data -> {
                        StableObject message = ResourcesManager.getJson("message");
                        if (rank.equals("NONE")) {
                            data.removeRank();
                            sender.sendMessage(Replacer.replaceIn(
                                    message.getStringProperty("command.rank.send.remove", null),
                                    "player", player.getDisplayName()));
                            player.sendMessage(message.getStringProperty("command.rank.receive.remove", null));
                        } else {
                            data.setRank(rank);
                            sender.sendMessage(Replacer.replaceIn(message.getStringProperty("command.rank.send.packageRank", null),
                                    "player", player.getDisplayName()));
                            player.sendMessage(Replacer.replaceIn(message.getStringProperty("command.rank.receive.packageRank", null),
                                    "rank", rank));
                        }
                    });
                })
                .register();
        new CommandAPICommand("packagerank")
                .withPermission(CommandPermission.OP)
                .withArguments(new PlayerArgument("player"))
                .withArguments(new StringArgument("rank").replaceSuggestions(ArgumentSuggestions.strings(
                        "VIP", "VIP_PLUS", "MVP", "MVP_PLUS"
                )))
                .withOptionalArguments(new ChatColorArgument("rankPlusColor"))
                .executes((sender, args) -> {
                    Player player = (Player) args.get(0);
                    String rank = (String) args.get(1);
                    Optional<Object> colorOptional = args.getOptional(2);
                    if (player == null || rank == null) {
                        return;
                    }
                    DataManager.getPlayerData(player).ifPresent(data -> {
                        colorOptional.ifPresent(color -> data.addProperty("rankPlusColor", ((ChatColor) color).name()));
                        data.setPackageRank(rank);

                        StableObject message = ResourcesManager.getJson("message");
                        sender.sendMessage(Replacer.replaceIn(message.getStringProperty("command.rank.send.packageRank", null),
                                "player", player.getDisplayName()));
                        player.sendMessage(Replacer.replaceIn(message.getStringProperty("command.rank.receive.packageRank", null),
                                "rank", rank));
                    });

                }).register();
        new CommandAPICommand("monthlypackagerank")
                .withPermission(CommandPermission.OP)
                .withArguments(new PlayerArgument("player"))
                .withArguments(new IntegerArgument("day", 1))
                .withOptionalArguments(new ChatColorArgument("monthlyRankColor"))
                .executes((sender, args) -> {
                    Player player = (Player) args.get(0);
                    int day = Integer.parseInt(String.valueOf(args.get(1)));
                    Optional<Object> colorOptional = args.getOptional(2);
                    if (player == null) {
                        return;
                    }
                    DataManager.getPlayerData(player).ifPresent(data -> {
                        colorOptional.ifPresent(color -> data.addProperty("rankPlusColor", ((ChatColor) color).name()));
                        data.setMonthlyPackageRank(day);

                        StableObject message = ResourcesManager.getJson("message");
                        sender.sendMessage(Replacer.replaceIn(message.getStringProperty("command.rank.send.monthlyPackageRank", null),
                                "player", player.getDisplayName(),
                                "day", day));
                        player.sendMessage(Replacer.replaceIn(message.getStringProperty("command.rank.receive.monthlyPackageRank", null),
                                "day", day));
                    });
                }).register();
    }

    @Override
    public void unregister() {
        CommandAPI.unregister("rank");
        CommandAPI.unregister("packagerank");
        CommandAPI.unregister("monthlypackagerank");
    }
}
